package com.hj.ct.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioGroup;

import com.hj.ct.R;
import com.hj.ct.fragment.BarChartFragment;
import com.hj.ct.fragment.MapFragment;
import com.hj.ct.fragment.SettingFragment;

public class MainActivity extends BaseActivity implements OnClickListener {
	private FragmentManager fragmentManager;
	private RadioGroup radioGroup;
	// private Fragment deviceListFragment;
	// private Fragment deviceMapFragment;
	private Fragment mapFragment;
	private Fragment chartFragment;
	private Fragment setFragment;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(null);
		setContentView(R.layout.activity_main);
		fragmentManager = getSupportFragmentManager();
		radioGroup = (RadioGroup) findViewById(R.id.rg_tab);
		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				changeFragment(checkedId);
			}
		});
		mapFragment = getInstanceByIndex(R.id.main_radio);
		if (mapFragment != null) {
			FragmentTransaction transaction = fragmentManager.beginTransaction();
			transaction.add(R.id.content, mapFragment);
			transaction.commit();
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {

	}

	@Override
	public void onClick(View v) {

	}

	public void hiddenAllFragment() {
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		// if (deviceListFragment != null && deviceListFragment.isAdded() && !deviceListFragment.isHidden()) {
		// transaction.hide(deviceListFragment);
		// }
		// if (deviceMapFragment != null && deviceMapFragment.isAdded() && !deviceMapFragment.isHidden()) {
		// transaction.hide(deviceMapFragment);
		// }
		if (mapFragment != null && mapFragment.isAdded() && !mapFragment.isHidden()) {
			transaction.hide(mapFragment);
		}
		if (chartFragment != null && chartFragment.isAdded() && !chartFragment.isHidden()) {
			transaction.hide(chartFragment);
		}
		if (setFragment != null && setFragment.isAdded() && !setFragment.isHidden()) {
			transaction.hide(setFragment);
		}
		transaction.commit();
	}

	public Fragment getInstanceByIndex(int index) {
		switch (index) {
			case R.id.main_radio:
				// switch (deviceMode) {
				// case MODE_LIST:
				// if (deviceListFragment == null) {
				// deviceListFragment = new DeviceListFragment();
				// }
				// return deviceListFragment;
				// case MODE_MAP:
				// if (deviceMapFragment == null) {
				// deviceMapFragment = new DeviceMapFragment();
				// }
				// return deviceMapFragment;
				// }
				if (mapFragment == null) {
					mapFragment = new MapFragment();
				}
				return mapFragment;
			case R.id.tuan_radio:
				if (chartFragment == null) {
					chartFragment = new BarChartFragment();
				}
				return chartFragment;
			case R.id.my_radio:
				if (setFragment == null) {
					setFragment = new SettingFragment();
				}
				return setFragment;
			default:
				return null;
		}
	}

	// public void onBackPressed() {
	// final CustomDialog dialog = new CustomDialog(this);
	// dialog.setTitile("提示");
	// dialog.setMessage("\n 确定要退出吗？                                  \n");
	// dialog.setLeftBtn("取消");
	// dialog.setRightBtn("确定");
	// dialog.setLeftBtnOnclickListener(new OnClickListener() {
	// public void onClick(View v) {
	// dialog.dismiss();
	// }
	// });
	// dialog.setRightBtnOnclickListener(new OnClickListener() {
	// public void onClick(View v) {
	// dialog.dismiss();
	// AndroidUtil.exits();
	// }
	// });
	// dialog.show();
	// }

	// 点击HOME键时程序进入后台运行
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		// 按下HOME键或返回键
		if (keyCode == KeyEvent.KEYCODE_HOME || keyCode == KeyEvent.KEYCODE_BACK) {
			moveTaskToBack(true);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private final int MODE_LIST = 0;
	private final int MODE_MAP = 1;
	private int deviceMode = MODE_LIST;

	public void changeDeviceMapModel() {
		deviceMode = MODE_MAP;
		changeFragment(R.id.main_radio);
	}

	public void changeDeviceListModel() {
		deviceMode = MODE_LIST;
		changeFragment(R.id.main_radio);
	}

	private void changeFragment(int indexId) {
		Fragment fragment = getInstanceByIndex(indexId);
		if (fragment != null) {
			hiddenAllFragment();
			FragmentTransaction transaction = fragmentManager.beginTransaction();
			if (fragment.isAdded()) {
				transaction.show(fragment);
			} else {
				transaction.add(R.id.content, fragment);
			}
			transaction.commit();
		}
	}

	public void handlerUI(int aciton, int code, Object... obj) {

	}
}
